<?php

class MY_Model Extends CI_Model
{
	protected $c_user;
	protected $c_gp;
	protected $c_sy;
	private $ci;
	public function __construct()
	{
		parent::__construct();
		$this->ci =& get_instance();
		$this->c_user = $this->get_user();
		$this->c_sy = $this->get_sy();
		$this->c_gp = $this->get_gp();
	}
	
	private function get_sy()
	{
		/*
		* id,sy_from,sy_to,is_set
		*/
		$sql = "SELECT * FROM school_years s WHERE s.is_set = 'yes' limit 1";
		$query = $this->db->query($sql);
		return $query->num_rows() >= 1 ? $query->row() : FALSE;
	}
	
	private function get_gp()
	{
		/*
		* gp_id,gp_code,gp_desc,gp_stat
		*/
		$sql = "SELECT gp_id,gp_code,gp_desc,gp_stat FROM grading_periods g WHERE g.is_set = 'yes' limit 1";
		$query = $this->db->query($sql);
		return $query->num_rows() >= 1 ? $query->row() : FALSE;
	}
	
	
	private function get_user()
	{
		$data['usertype'] = $this->ci->session->userdata('userType');
		$data['userid'] = $this->ci->session->userdata('userid');
		return (object)$data;
	}
	
	
	/* Dynamic Get Record */
	/* table - table name */
	/* config (array) - filter, like, order, group, fields */
	/* SLY | Added : 8/6/2014*/
	public function get_record($table = false, $config = array(), $debug = false)
	{
		if($table == false)
		{
			$table = $this->_table;
		}
		
		//CONFIGURATION
		//$config['fields'] = specific fields
		//$config['where'] = AND conditions
		//$config['or_where'] = OR conditions
		//$config['like'] = LIKE conditions
		//$config['or_like'] = OR LIKE conditions
		// $config['join'][] = array(
			// "table" = "TABLE NAME",
			// "on"	= "ON STRING",
			// "type"  = "LEFT,RIGHT"
		// )
		//$config['not_in'] = array(
			// "field" => "field_name",
			// "data" => "ARRAY OR STRING"
		// )
		//$config['group'] = GROUP BY conditions
		//$config['order'] = ORDER BY conditions
		//$config['start'] = LIMIT START conditions
		//$config['limit'] = LIMIT END conditions
		//$config['all'] = true or false : return all removes limit
		//$config['count'] = true or false : return count not the row
		//$config['array'] = true or false : return array instead of object
		//$config['single'] = true or false : return single record
		
		
		
		//FIELDS CONFIGURATION
		if(isset($config['fields']) && $config['fields'] != false)
		{
			$this->db->select($config['fields']);
		}
		else
		{
			$this->db->select('*');
		}
		
		$this->db->from($table); //FROM TABLE
		
		//WHERE
		if(isset($config['where']) && $config['where'] != false)
		{
			$this->db->where($config['where']);
		}
		
		//OR WHERE
		if(isset($config['or_where']) && $config['or_where'] != false)
		{
			$this->db->or_where($config['or_where']);
		}
		
		//LIKE
		if(isset($config['like']) && $config['like'] != false)
		{
			$this->db->like($config['like']);
		}
		
		//OR LIKE
		if(isset($config['or_like']) && $config['or_like'] != false)
		{
			$this->db->or_like($config['or_like']);
		}
		
		//WHERE NOT IN
		//$this->db->where_not_in('username', $names);
		if(isset($config['not_in']) && $config['not_in'] != false)
		{
			$xnot_in = $config['not_in'];
			if(isset($xnot_in['field']) && $xnot_in['field'] != false)
			{
				if(isset($xnot_in['data']) && $xnot_in['data'] != false)
				{
					if(is_array($xnot_in['data'])){
						$this->db->where_not_in($xnot_in['field'], $xnot_in['data']);
					}
					else if(is_string($xnot_in['data'])){
						
						$_xfilter = $xnot_in['field'].' NOT IN ( ' .$xnot_in['data']. ' ) ';
						
						// $this->db->where(“events.id NOT IN (SELECT event_id2 FROM related_events”);
						
						$this->db->where($_xfilter);
					}
				}
			}
		}
		
		//JOIN STATEMENTS
		if(isset($config['join']) && is_array($config['join']))
		{
			foreach($config['join'] as $join)
			{
				if($join['table'] != "")
				{
					$this->db->join($join['table'], $join['on'],strtoupper($join['type']));
				}
			}
		}
		
		//GROUP
		if(isset($config['group']) && $config['group'] != false)
		{
			$this->db->group_by($config['group']);
		}
		
		//ORDER BY
		if(isset($config['order']) && $config['order'] != false)
		{
			$this->db->order_by($config['order']);
		}
		
		//CHECK IF ALL IF TRUE
		if(isset($config['all']) && $config['all'] == true)
		{
		}
		else
		{
			//LIMIT START END
			if(isset($config['limit']) && isset($config['start']))
			{
				$this->db->limit($config['limit'], $config['start']);
			}
			else
			{
				if(isset($config['limit']))
				{
					$this->db->limit($config['limit']);
				}
			}
		}
		
		$query = $this->db->get(); //EXECUTE QUERY
	
		//CHECK IF DEBUG
		if($debug)
		{
			vp($this->db->last_query());
			vd('');
		}
		
		//CHECK IF COUNT TRUE
		if(isset($config['count']) && $config['count'] == true)
		{
			return $query->num_rows();
		}
		else
		{	
			//CHECK IF SINGLE
			if(isset($config['single']) && $config['single'] == true)
			{
				if(isset($config['array']) && $config['array'] == true)
				{
					return $query->num_rows > 0 ? $query->first_row('array') : false;
				}
				else
				{
					return $query->num_rows > 0 ? $query->row() : false;
				}
			}
			else
			{
				if(isset($config['array']) && $config['array'] == true)
				{
					return $query->num_rows > 0 ? $query->result_array : false;
				}
				else
				{
					return $query->num_rows > 0 ? $query->result() : false;
				}
			}
		}
		
		return false;
	}
	
	
	
	
	
	
	
	

}